import { _decorator, Component, IVec3, Node, tween, Tween, Vec3 } from 'cc';
import { EntityTypeEnum, IActor, IBullet } from '../../Common';
import { EntityStateEnum, EventEnum } from '../../Enum';
import DataManager from '../../Global/DataManager';
import { rad2Angle } from '../../Utils';
import { WeaponManager } from '../Weapon/WeaponManager';
import { BulletStateMachine } from './BulletStateMachine';
import { EntityManager } from '../../Base/EntityManager';
import EventManager from '../../Global/EventManager';
import { ExplosionManager } from '../Explosion/ExplosionManager';
import { ObjectPoolManager } from '../../Global/ObjectPoolManager';
const { ccclass, property } = _decorator;

@ccclass('BulletManager')
export class BulletManager extends EntityManager {
    
    type: EntityTypeEnum;
    id: number;
      private targetPos:Vec3;
       private tw:Tween<unknown>;
    //初始化
    init(data: IBullet) {
        this.targetPos = undefined;
        this.type = data.type;
        this.id = data.id;
        this.fsm = this.addComponent(BulletStateMachine);
        this.fsm.init(data.type);
        console.log("发射子弹");

        this.state = EntityStateEnum.Idle;

        // this.node.setPosition(data.position.x, data.position.y, data.position.z);//设置位置
        this.node.active=false;

        //监听事件
        EventManager.Instance.on(EventEnum.ExplosionBorn, this.handleExplosionBorn, this);
    }
    protected onDisable(): void {
        EventManager.Instance.off(EventEnum.ExplosionBorn, this.handleExplosionBorn, this);
    }
    protected onDestroy(): void {
        EventManager.Instance.off(EventEnum.ExplosionBorn, this.handleExplosionBorn, this);
    }

    render(data: IBullet) {
        this.renderPos(data);
        this.renderDire(data);
    }
    renderPos(data: IBullet){
        const {position } = data;
        // this.node.setPosition(position.x, position.y, position.z);//设置位置

        const newPos=new Vec3(position.x,position.y,position.z);
        if(!this.targetPos){
            this.node.active=true;
            this.node.setPosition(newPos);
            this.targetPos=new Vec3(newPos);
        }else if(!this.targetPos.equals(newPos)){//只有位置改变大于0.1才缓动
            this.tw?.stop();//开始缓动前停止其他缓动
            this.node.setPosition(this.targetPos);
            this.targetPos.set(newPos);
            this.tw=tween(this.node)
            .to(0.1,{position:this.targetPos})
            .call(()=>{
            })  
            .start();
        }
    }
    renderDire(data: IBullet){
        const { direction } = data;

        const side = Math.sqrt(direction.x * direction.x + direction.y * direction.y);
        const rad = direction.x > 0 ? Math.asin(direction.y / side) : Math.asin(-direction.y / side);
        const angle = direction.x > 0 ? rad2Angle(rad) : rad2Angle(rad) + 180;

        this.node.setRotationFromEuler(0, 0, angle);
    }

    handleExplosionBorn(id: number, { x, y, z }: IVec3) {
        if (id !== this.id) {
            return;
        }

        //创建爆炸
        const explosion = ObjectPoolManager.Instance.get(EntityTypeEnum.Explosion);
        // explosion.parent = DataManager.Instance.stage;
        const em=explosion.getComponent(ExplosionManager)|| explosion.addComponent(ExplosionManager);
        em.init(
            EntityTypeEnum.Explosion,
            { x, y, z },
        );
        DataManager.Instance.bulletMap.delete(this.id);
        // this.node.destroy();
       ObjectPoolManager.Instance.ret(this.node);
    }
}

